package com.hfut.eeg.common.component;


import com.hfut.eeg.common.util.EEGUtil;
import com.hfut.eeg.dto.EerWaves;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;


@Component
@Scope("singleton")
public class Receivecache implements ApplicationContextAware {
    private static final Logger LOGGER = LoggerFactory.getLogger(EEGcache.class);

    @Autowired
    ReceiveThread receiveThread;

    private ApplicationContext applicationContext;
    //表示缓冲区的阈值
    private static final int threshold = 5000;

    private ArrayList<ArrayList>Ffts = new ArrayList<>();
    private ArrayList<EerWaves>ErrWaves = new ArrayList<>();
    private ArrayList<ArrayList>BrainChannel = new ArrayList<>();
    private ArrayList<ArrayList>Energy = new ArrayList<>();

    private String EegId = null;

    public void reset(){
        Ffts.clear();
        ErrWaves.clear();
        BrainChannel.clear();
        Energy.clear();
        EegId = null;
    }
    //进行向缓冲区里面进行添加数据
    public boolean addFftToPool(ArrayList<ArrayList> Fft,String eegId){
        EegId = eegId;
        if(Fft==null||Fft.size()==0){
            LOGGER.warn("Fft数据为空");
            return false;
        }
        if(Ffts.size()==0){
            Ffts = Fft;
        }else{
            for(int i=0;i<Fft.size();i++){
                Ffts.get(i).addAll(Fft.get(i));
            }
        }

        if(Ffts.get(0).size()>=threshold){
            receiveThread = (ReceiveThread) applicationContext.getBean("receiveThread");
            try{
                flushPool();
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public boolean addErrWavesToPool(ArrayList<EerWaves>errWaves){
        if(ErrWaves==null||errWaves.size()==0){
            LOGGER.warn("异常波为空，错误");
            return false;
        }

        if(ErrWaves.size()==0){
            ErrWaves = errWaves;
        }
        else{
            ErrWaves.addAll(errWaves);
        }
        return true;
    }
    public boolean addBrainChannel(ArrayList<ArrayList>brainChannel){
        if(brainChannel==null||brainChannel.size()==0){
            LOGGER.warn("brainChannel数据为空");
            return false;
        }
        if(BrainChannel.size()==0)
            BrainChannel = brainChannel;
        else{
            for(int i=0;i<brainChannel.size();i++){
                BrainChannel.get(i).addAll(brainChannel.get(i));
            }
        }
        return false;
    }

    public boolean addEnergyChannel(ArrayList energy){
        if(energy==null||energy.size()==0){
            LOGGER.warn("energy能量值为空");
            return false;
        }
        if(Energy.size()==0){
            for(int i=0;i<5;i++) {
                ArrayList arr = new ArrayList();
                arr.add(energy.get(i));
                Energy.add(arr);
            }
        }
        else{
            for(int i=0;i<5;i++){
                Energy.get(i).add(energy.get(i));
            }
        }
        return true;
    }

    public static ArrayList<EerWaves> deepCopy(ArrayList<EerWaves> errWaves) throws CloneNotSupportedException {
        ArrayList<EerWaves> result = new ArrayList<>();
        for(EerWaves errWave:errWaves){
            result.add(errWave.clone());
        }
        return result;
    }

    public void flushPool() throws CloneNotSupportedException {
        LOGGER.info("fft大小"+Ffts.get(0).size());
        LOGGER.info("ErrWaves大小"+ErrWaves.size());
        LOGGER.info("Energy大小"+Energy.get(0).size());
        LOGGER.info("BrainChannel大小"+BrainChannel.get(0).size());
        receiveThread.setFfts(EEGUtil.deepCopy(Ffts));
        receiveThread.setBrainChannel(EEGUtil.deepCopy(BrainChannel));
        receiveThread.setEnergy(EEGUtil.deepCopy(Energy));
        receiveThread.setErrWaves(deepCopy(ErrWaves));
        Ffts.clear();
        BrainChannel.clear();
        Energy.clear();
        ErrWaves.clear();
        receiveThread.setEegId(EegId);
        new Thread(receiveThread).start();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
